<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of CookieDao
 *
 * @author Nhân
 */
require_once 'DbConnection.php';

class CookieDao {

    public function addCookie($cookie) {        
        $conn = DbConnection::getInstance()->openConnection();
        if ($conn != null) {
            if (is_array($cookie)) {
                foreach ($cookie as $c) {
                    $strQuery = "INSERT INTO Cookie(`name`, `image`, `price`, `description`, `cookieType`) VALUES('" . $c->getName() . "', '" .
                        $c->getImage() . "', '" . $c->getPrice() . "', '" . $c->getDescription() . "', '" . $c->getCookieType() . "')";
                    mysql_query($strQuery, $conn);
                }
            } else {
                $strQuery = "INSERT INTO Cookie(`name`, `image`, `price`, `description`, `cookieType`) VALUES('" . $cookie->getName() . "', '" .
                        $cookie->getImage() . "', '" . $cookie->getPrice() . "', '" . $cookie->getDescription() . "', '" . $cookie->getCookieType() . "')";
                mysql_query($strQuery, $conn);
            }
        }
    }
    
    public function updateCookie($cookie) {
        $conn = DbConnection::getInstance()->openConnection();
        if ($conn != null) {
            if (is_array($cookie)) {
                foreach ($cookie as $c) {
                    $strQuery = "UPDATE Cookie SET `name` = '". $c->getName() . "', `image` = '" .
                        $c->getImage() . "', `price` = '" . $c->getPrice() . "', `description` = '" . $c->getDescription() . "', `cookieType` = `" . $c->getCookieType() . "` ". 
                            "WHERE `id` = '" . $c->getId() . "'";
                    mysql_query($strQuery, $conn);
                }
            } else {
                $strQuery = "UPDATE Cookie SET `name` = '". $cookie->getName() . "', `image` = '" .
                        $cookie->getImage() . "', `price` = '" . $cookie->getPrice() . "', `description` = '" . $cookie->getDescription() . "', `cookieType` = '" . $cookie->getCookieType() . "' ". 
                            "WHERE `id` = '" . $cookie->getId() . "'";
                    mysql_query($strQuery, $conn);
            }
        }
    }
    
    public function deleteCookie($id) {
        $conn = DbConnection::getInstance()->openConnection();
        if ($conn != null) {
            if (is_array($id)) {
                foreach ($id as $i) {
                    $strQuery = "DELETE Cookie WHERE `id` = '" . $i . "'";
                    mysql_query($strQuery, $conn);
                }
            } else {
                $strQuery = "DELETE Cookie WHERE `id` = '" . $id . "'";
                    mysql_query($strQuery, $conn);
            }
        }
    }
    
    public function findCookieAsCookieType($cookieType) {
        $conn = DbConnection::getInstance()->openConnection();
        $result = array();
        if ($conn != null) {
            $strQuery = "SELECT * FROM Cookie WHERE `cookieType` = '" . $cookieType . "'";
            $rows = mysql_query($strQuery, $conn);
            while ($row = mysql_fetch_array($rows)) {
                $cookie = new Cookie();
                $cookie->setId($row['id']);
                $cookie->setImage($row['image']);
                $cookie->setCookieType($row['cookieType']);
                $cookie->setDescription($row['description']);
                $cookie->setName($row['name']);
                $cookie->setPrice($row['price']);
                $result[] = $cookie;
            }
        }
        return $result;
    }
    
    public function findCookieAsCookieTypeWithPaging($cookieType, $from, $to) {
        $conn = DbConnection::getInstance()->openConnection();
        $result = array();
        if ($conn != null) {
            $strQuery = "SELECT * FROM Cookie LIMIT " . $from . "," . $to . " WHERE `cookieType` = '" . $cookieType . "'";
            $rows = mysql_query($strQuery, $conn);
            while ($row = mysql_fetch_array($rows)) {
                $cookie = new Cookie();
                $cookie->setId($row['id']);
                $cookie->setImage($row['image']);
                $cookie->setCookieType($row['cookieType']);
                $cookie->setDescription($row['description']);
                $cookie->setName($row['name']);
                $cookie->setPrice($row['price']);
                $result[] = $cookie;
            }
        }
        return $result;
    }
}

?>
